CREATE PROCEDURE News_FindAll
AS
BEGIN
    Select 
    NewsId,
    ApplicationId,
    CategoryId,
    Name,
    UrlFriendly,
    Description,
    Body,
    Picture,
    CreatedById,
    CreatedBy,
    ReplyTo,
    IsActive,
    SortIndex,
    FileAttachmentId,
    DateCreated,
    DateModified,
    IsDelete
    from News
END
GO

CREATE PROCEDURE News_FindById
@NewsId int
AS
BEGIN
    Select * from News where NewsId = @NewsId
END
GO

CREATE PROCEDURE News_FindByName
@name nvarchar(50)
AS
BEGIN
    Select * from News where NewsId = @name
END
GO

CREATE PROCEDURE News_Insert
        @NewsId int ,
        @ApplicationId int ,
        @CategoryId int ,
        @Name nvarchar (250),
        @UrlFriendly nvarchar (255),
        @Description nvarchar (250),
        @Body ntext (1073741823),
        @Picture nvarchar (255),
        @CreatedById int ,
        @CreatedBy nvarchar (50),
        @ReplyTo int ,
        @IsActive bit ,
        @SortIndex int ,
        @FileAttachmentId int ,
        @DateCreated datetime ,
        @DateModified datetime ,
        @IsDelete bit 
AS
BEGIN
INSERT INTO News
(
        NewsId,
        ApplicationId,
        CategoryId,
        Name,
        UrlFriendly,
        Description,
        Body,
        Picture,
        CreatedById,
        CreatedBy,
        ReplyTo,
        IsActive,
        SortIndex,
        FileAttachmentId,
        DateCreated,
        DateModified,
        IsDelete
)
VALUES
(
        @NewsId,
        @ApplicationId,
        @CategoryId,
        @Name,
        @UrlFriendly,
        @Description,
        @Body,
        @Picture,
        @CreatedById,
        @CreatedBy,
        @ReplyTo,
        @IsActive,
        @SortIndex,
        @FileAttachmentId,
        @DateCreated,
        @DateModified,
        @IsDelete
)
select @NewsId = @@IDENTITY
END
GO

CREATE PROCEDURE News_Update
        @NewsId int ,
        @ApplicationId int ,
        @CategoryId int ,
        @Name nvarchar (250),  
        @UrlFriendly nvarchar (255),  
        @Description nvarchar (250),  
        @Body ntext (1073741823),  
        @Picture nvarchar (255),  
        @CreatedById int ,
        @CreatedBy nvarchar (50),  
        @ReplyTo int ,
        @IsActive bit ,
        @SortIndex int ,
        @FileAttachmentId int ,
        @DateCreated datetime ,
        @DateModified datetime ,
        @IsDelete bit
AS
BEGIN
UPDATE  News SET
    ApplicationId = @ApplicationId,
    CategoryId = @CategoryId,
    Name = @Name,
    UrlFriendly = @UrlFriendly,
    Description = @Description,
    Body = @Body,
    Picture = @Picture,
    CreatedById = @CreatedById,
    CreatedBy = @CreatedBy,
    ReplyTo = @ReplyTo,
    IsActive = @IsActive,
    SortIndex = @SortIndex,
    FileAttachmentId = @FileAttachmentId,
    DateCreated = @DateCreated,
    DateModified = @DateModified,
    IsDelete = @IsDelete
    WHERE NewsId = @NewsId
END
GO

CREATE PROCEDURE News_Delete
    @NewsId int
AS
BEGIN
    DELETE FROM News where NewsId = @NewsId
END
GO

CREATE PROCEDURE News_Paging
    @PageSize int, 
    @PageNumber int
    AS 
    declare @RowStart int
    declare @RowEnd int
    if @PageNumber > 0 
    BEGIN	
    set @PageNumber = @PageNumber - 1
    set @RowStart = @PageSize * @PageNumber + 1;
    set @RowEnd = @RowStart  + @PageSize - 1;
    SELECT TotalRowCount = COUNT(NewsId) FROM News;
    WITH MyTable as 
    (
    SELECT * ,  ROW_NUMBER() OVER (order by SortIndex) as RowNumber From  News
    )
    SELECT * from MyTable where RowNumber >= @RowStart and RowNumber <= @RowEnd
END
GO

CREATE proc News_SetActive
@NewsId int
AS
declare @Type bit
SELECT @Type =  IsActive from News where NewsId = @NewsId
BEGIN
IF(@Type = 1)
Update News set IsActive = 0 where NewsId = @NewsId
else
Update News set IsActive = 1 where NewsId = @NewsId
END
GO

CREATE proc News_SetSortIndex
@NewsId int, 
@Type int
AS
declare @CurrentSort int
select @CurrentSort = SortIndex from  News where NewsId = @NewsId
BEGIN
IF(@Type = 1)
Update News set SortIndex =  @CurrentSort + 1 where NewsId = @NewsId
else
Update News set SortIndex =  @CurrentSort - 1 where NewsId = @NewsId
END
GO

CREATE PROCEDURE News_RemoveSelectedItem
    @NewsId int
AS
BEGIN
    UPDATE News SET IsDeleted = 1 WHERE NewsId = @NewsId
END
GO

CREATE PROCEDURE News_RestoreSelectedItem
    @NewsId int
AS
BEGIN
    UPDATE News SET IsDeleted = 0 WHERE NewsId = @NewsId
END
GO

CREATE PROCEDURE News_RemoveAll
    @NewsId int
AS
BEGIN
    UPDATE News SET IsDeleted = 1
END
GO

CREATE PROCEDURE News_RestoreAll
    @NewsId int
AS
BEGIN
    UPDATE News SET IsDeleted = 0
END
GO

